import os
from pathlib import Path

from pydantic import PostgresDsn, computed_field
from pydantic_settings import BaseSettings

BASE_DIR = Path(__file__).resolve().parent.parent.parent  # 向上再跳一层
class Settings(BaseSettings):
    API_V1_STR: str = "/api"
    PROJECT_NAME: str = "商城"
    PROJECT_DESC: str = "这是一个神奇的网站"
    VERSION: str = "1.0.0"
    ENVIRONMENT: str = "development"

    # 数据库配置
    DATABASE_USER: str
    DATABASE_PASSWORD: str
    DATABASE_HOST: str
    DATABASE_PORT: str = "5432"
    DATABASE_NAME: str

    @computed_field  # ❶ 计算属性，不再从环境变量读
    @property
    def SQLALCHEMY_DATABASE_URI(self) -> PostgresDsn:
        return PostgresDsn.build(
            scheme="postgresql+pg8000",
            username=self.DATABASE_USER,
            password=self.DATABASE_PASSWORD,
            host=self.DATABASE_HOST,
            port=int(self.DATABASE_PORT),
            path=self.DATABASE_NAME,
        )

    # 安全配置
    SECRET_KEY: str
    ALGORITHM: str = "HS256"
    ACCESS_TOKEN_EXPIRE_MINUTES: int = 30

    # 日志配置
    LOG_LEVEL: str = os.getenv("LOG_LEVEL", "INFO")
    LOG_FILE: str = os.getenv("LOG_FILE", "app.log")
    class Config:
        case_sensitive = True
        env_file = BASE_DIR / ".env"
settings = Settings()
